const jwt = require('jsonwebtoken');

const key = '0215zuiniubi'
// 生成token函数
let signToken = (data,time='1d')=>jwt.sign(data,key,{ expiresIn:time });

// 验证token函数
let verifyToken = (token)=>{
   return  jwt.verify(token,key,(err,data)=>{
        if(err){
            if(err.name === 'TokenExpiredError'){
                throw new Error('token过期')
            }else{
                throw new Error('token非法')
            }
        }else{
            return data;
        }
    })
}

module.exports = {
    signToken,
    verifyToken
}



// let token = signToken({name:"laowang"});

// console.log(token);

// let res = verifyToken('abc');

// console.log(res);
